var base = null
var azurCtrl
var utils
var config
func _ready():
	pass
func _init():
	call_deferred("azurlineInit")
	pass
func _connect():
	pass

func azurlineInit():
	if globalData.infoDs.has("g_azurlaneBase"):
		base = globalData.infoDs["g_azurlaneBase"]
	if base != null:
		#初始化
		azurCtrl = globalData.infoDs["g_azurlane"]
		utils = globalData.infoDs["g_azurlaneUtils"]
		base.connect("onSaveGame",self,"onSaveGame")
		azurCtrl.connect("onNewGame",self,"newGame")
		azurCtrl.connect("resetData", self, "resetData")
		azurCtrl.connect("onLoadGame",self,"loadGame")

		yield(sys.get_tree().create_timer(0.2), "timeout")
		config = load("%s/g_azurlaneMap/MapConfig.gd" % [sys.getBasePath()]).new()

func newGame():
	sys.main.connect("onBattleStart",self,"onBattleStart")
	sys.main.connect("ups",self,"ups")

func resetData():
	pass

func onSaveGame():
	pass

func loadGame():
	sys.main.connect("onBattleStart",self,"onBattleStart")		
	sys.main.connect("ups",self,"ups")		

func ups():
	pass

func onBattleStart():
	for i in mapItems:
		if i.isObstacle:continue
		var cell = i.cells[0]
		var matCha = sys.main.matCha(cell)
		if matCha == null:continue
		match i.type:
			"hudun":
				matCha.changeShield(matCha.att.maxHp)
			"fuhuo":
				matCha.addBuff(config.loadBuff("b_map_fuHuo"))


var mapItems = []
func createObstacle(file, cells = []):
	var tr = utils.createImgBtn("icons/obstacle/%s.png"%file)
	sys.main.map.add_child(tr)
	tr.rect_position = sys.main.map.map_to_world(cells[0]) + obstacleRect.get(file)
	tr.connect("onPressed", self, "onPressed", [""])
	mapItems.append({"obj":tr, "isObstacle":true, "cells":cells, "type":""})
	for i in cells:
		moveCha(i)
		sys.main.obstacles.append(i)

func createSpecCell(file, cell):
	var tr = utils.createImgBtn("icons/obstacle/%s.png"%file)
	sys.main.map.add_child(tr)
	tr.rect_position = sys.main.map.map_to_world(cell) + config.specCellDic.get(file).rect
	tr.rect_scale = config.specCellDic.get(file).scale
	tr.connect("onPressed", self, "onPressed", [file])
	mapItems.append({"obj":tr, "isObstacle":false, "cells":[cell], "type":file})
	moveCha(cell)

var sortedCell
func moveCha(cell):
	var matCha = sys.main.matCha(cell)
	if matCha != null:
		sortedCell = cell
		utils.cellsLeft.sort_custom(self, "sortCells")
		for i in utils.cellsLeft:
			if sys.main.isMatin(i) and sys.main.matCha(i) == null:
				matCha.setCell(i)

func sortCells(a, b):
	return utils.cellRan(sortedCell, a) <= utils.cellRan(sortedCell, b)		

const obstacleRect = {
	"bingchuanA1x2": Vector2(-50, -80)
}

func onPressed(type):
	if type != "":
		sys.newBaseMsg("特殊单元格", config.specCellDic.get(type).desc)
	else:
		sys.newBaseMsg("障碍物", "不可通行")

func createMap27():
	for i in mapItems:
		i.obj.queue_free()
	mapItems.clear()	
	createObstacle("bingchuanA1x2", [Vector2(3,0), Vector2(4,0)])
	createObstacle("bingchuanA1x2", [Vector2(3,1), Vector2(4,1)])
	createObstacle("bingchuanA1x2", [Vector2(3,4), Vector2(4,4)])
	createObstacle("bingchuanA1x2", [Vector2(3,5), Vector2(4,5)])
	createSpecCell("juji", Vector2(0, 0))
	createSpecCell("lengque", Vector2(0, 5))
	createSpecCell("fuhuo", Vector2(3, 2))